﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace Client
{
    public class SerializableClass
    {
        public static byte[] Serialization(object message) {
            if(message == null) { return null; }

            //如果成功
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();

            bf.Serialize(ms, message);

            byte[] result = new byte[ms.Length];
            Buffer.BlockCopy(ms.GetBuffer(), 0, result, 0, result.Length);

            ms.Close();
            return result;
        }


        public static object DeSerialization(byte[] value)
        {
            if (value == null||value.Length==0) { return null; }

            //如果成功
            MemoryStream ms = new MemoryStream(value);
            BinaryFormatter bf = new BinaryFormatter();

            object result = bf.Deserialize(ms);

            ms.Close();
            return result;
        }
    }
}
